<template>
  <ion-page>
    <ion-header class="ion-no-border modern-header">
      <ion-toolbar class="gradient-toolbar">
        <ion-title class="page-title">
          <div class="title-content">
            <span class="title-text">📚 书城</span>
            <span class="title-subtitle">发现好书，享受阅读</span>
          </div>
        </ion-title>
      </ion-toolbar>
    </ion-header>
    
    <ion-content :fullscreen="true" class="modern-content">
      <!-- 现代化搜索栏 -->
      <div class="search-container">
        <ion-searchbar 
          placeholder="搜索书名、作者..." 
          @click="() => router.push('/tabs/search')"
          class="modern-searchbar"
          search-icon="search-outline"
        ></ion-searchbar>
      </div>

      <!-- 借阅榜卡片 -->
      <ion-card class="ranking-card fade-in">
        <ion-card-header class="card-header">
          <div class="header-content">
            <div class="header-left">
              <ion-icon name="trophy-outline" class="trophy-icon"></ion-icon>
              <span class="card-title">借阅排行榜</span>
            </div>
            <div class="header-right" @click="() => router.push('/tabs/more')">
              <span class="more-text">更多</span>
              <ion-icon name="chevron-forward-outline" class="more-icon"></ion-icon>
            </div>
          </div>
        </ion-card-header>
        
        <ion-card-content class="card-content">
          <!-- 现代化分段控件 -->
          <ion-segment 
            @ionChange="segmentChanged($event)" 
            color="primary" 
            :value="svalue"
            class="modern-segment"
          >
            <ion-segment-button value="编程" class="segment-btn">
              <ion-label>💻 编程</ion-label>
            </ion-segment-button>
            <ion-segment-button value="社科" class="segment-btn">
              <ion-label>🏛️ 社科</ion-label>
            </ion-segment-button>
            <ion-segment-button value="历史" class="segment-btn">
              <ion-label>📜 历史</ion-label>
            </ion-segment-button>
            <ion-segment-button value="小说" class="segment-btn">
              <ion-label>📖 小说</ion-label>
            </ion-segment-button>
          </ion-segment>
          
          <!-- 图书列表 -->
          <div class="books-container">
            <div v-show="svalue==='编程'" class="book-list slide-up">
              <div class="book-row" v-for="(item,index) of data1" :key="index">
                <div class="book-item" v-for="(book, bookIndex) of item" :key="bookIndex">
                  <div class="book-card" @click="() => router.push('/tabs/book')">
                    <div class="book-image-container">
                      <img :src="book.img" class="book-image" />
                      <div class="book-overlay">
                        <ion-icon name="eye-outline" class="view-icon"></ion-icon>
                      </div>
                    </div>
                    <div class="book-info">
                      <div class="rank-badge" :class="getRankClass(index * 2 + bookIndex + 1)">
                        {{ index * 2 + bookIndex + 1 }}
                      </div>
                      <div class="book-details">
                        <div class="book-title">{{ book.name }}</div>
                        <div class="book-author">{{ book.author }}</div>
                        <div class="book-stats">
                          <ion-icon name="heart-outline" class="stat-icon"></ion-icon>
                          <span class="stat-text">{{ Math.floor(Math.random() * 1000) + 100 }}</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            
            <!-- 其他分类的图书列表 -->
            <div v-show="svalue==='社科'" class="book-list slide-up">
              <div class="book-row" v-for="(item,index) of data3" :key="index">
                <div class="book-item" v-for="(book, bookIndex) of item" :key="bookIndex">
                  <div class="book-card" @click="() => router.push('/tabs/book')">
                    <div class="book-image-container">
                      <img :src="book.img" class="book-image" />
                      <div class="book-overlay">
                        <ion-icon name="eye-outline" class="view-icon"></ion-icon>
                      </div>
                    </div>
                    <div class="book-info">
                      <div class="rank-badge" :class="getRankClass(index * 2 + bookIndex + 1)">
                        {{ index * 2 + bookIndex + 1 }}
                      </div>
                      <div class="book-details">
                        <div class="book-title">{{ book.name }}</div>
                        <div class="book-author">{{ book.author }}</div>
                        <div class="book-stats">
                          <ion-icon name="heart-outline" class="stat-icon"></ion-icon>
                          <span class="stat-text">{{ Math.floor(Math.random() * 1000) + 100 }}</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            
            <div v-show="svalue==='历史'" class="book-list slide-up">
              <div class="book-row" v-for="(item,index) of data4" :key="index">
                <div class="book-item" v-for="(book, bookIndex) of item" :key="bookIndex">
                  <div class="book-card" @click="() => router.push('/tabs/book')">
                    <div class="book-image-container">
                      <img :src="book.img" class="book-image" />
                      <div class="book-overlay">
                        <ion-icon name="eye-outline" class="view-icon"></ion-icon>
                      </div>
                    </div>
                    <div class="book-info">
                      <div class="rank-badge" :class="getRankClass(index * 2 + bookIndex + 1)">
                        {{ index * 2 + bookIndex + 1 }}
                      </div>
                      <div class="book-details">
                        <div class="book-title">{{ book.name }}</div>
                        <div class="book-author">{{ book.author }}</div>
                        <div class="book-stats">
                          <ion-icon name="heart-outline" class="stat-icon"></ion-icon>
                          <span class="stat-text">{{ Math.floor(Math.random() * 1000) + 100 }}</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            
            <div v-show="svalue==='小说'" class="book-list slide-up">
              <div class="book-row" v-for="(item,index) of data5" :key="index">
                <div class="book-item" v-for="(book, bookIndex) of item" :key="bookIndex">
                  <div class="book-card" @click="() => router.push('/tabs/book')">
                    <div class="book-image-container">
                      <img :src="book.img" class="book-image" />
                      <div class="book-overlay">
                        <ion-icon name="eye-outline" class="view-icon"></ion-icon>
                      </div>
                    </div>
                    <div class="book-info">
                      <div class="rank-badge" :class="getRankClass(index * 2 + bookIndex + 1)">
                        {{ index * 2 + bookIndex + 1 }}
                      </div>
                      <div class="book-details">
                        <div class="book-title">{{ book.name }}</div>
                        <div class="book-author">{{ book.author }}</div>
                        <div class="book-stats">
                          <ion-icon name="heart-outline" class="stat-icon"></ion-icon>
                          <span class="stat-text">{{ Math.floor(Math.random() * 1000) + 100 }}</span>
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </ion-card-content>
      </ion-card>
    </ion-content>
  </ion-page>
</template>

<script lang="ts">
import { defineComponent } from 'vue';
import { useRouter } from 'vue-router';
import {
  IonHeader,
  IonToolbar,
  IonTitle,
  IonPage,
  IonCard,
  IonIcon,
  IonCardContent,
  IonContent,
  IonSearchbar,
  IonCardHeader,
  IonSegment,
  IonSegmentButton,
  IonLabel,
} from '@ionic/vue';

export default defineComponent({
  name: 'HomePage',
  components: {
    IonHeader,
    IonToolbar,
    IonTitle,
    IonPage,
    IonCard,
    IonIcon,
    IonCardContent,
    IonContent,
    IonSearchbar,
    IonCardHeader,
    IonSegment,
    IonSegmentButton,
    IonLabel,
  },
  data() {
    return {
      svalue: '编程',
      data1: [
        [
          {
            name: 'Java并发编程实战',
            author: 'Brian Goetz',
            img: require('../../public/assets/images/3.jpg')
          },
          {
            name: '零基础学C++',
            author: '明日科技',
            img: require('../../public/assets/images/4.jpg')
          },
        ],
        [
          {
            name: 'C#从入门到精通',
            author: '明日科技',
            img: require('../../public/assets/images/5.jpg')
          },
          {
            name: '零基础学Python',
            author: '明日科技',
            img: require('../../public/assets/images/6.jpg')
          }
        ],
        [
          {
            name: '程序员数学',
            author: '保罗·奥兰德',
            img: require('../../public/assets/images/7.jpg')
          },
          {
            name: '看漫画学Python',
            author: '赵大羽',
            img: require('../../public/assets/images/8.jpg')
          }
        ]
      ],
      data2: [
        [
          {
            name: '魔兽战神',
            img: require('../../public/assets/images/9.jpg')
          },
          {
            name: '笑面难为',
            img: require('../../public/assets/images/10.jpg')
          },
          {
            name: '狂人日记',
            img: require('../../public/assets/images/11.jpg')
          },
          {
            name: '边城',
            img: require('../../public/assets/images/12.jpg')
          }
        ],
        [
          {
            name: '文学是什么',
            img: require('../../public/assets/images/13.jpg')
          },
          {
            name: 'Python入门',
            img: require('../../public/assets/images/6.jpg')
          },
          {
            name: '程序员数学',
            img: require('../../public/assets/images/7.jpg')
          },
          {
            name: '漫画Python',
            img: require('../../public/assets/images/8.jpg')
          }
        ]
      ],
      data3: [
        [
          {
            name: '文学是什么',
            author: '明日科技',
            img: require('../../public/assets/images/13.jpg')
          },
          {
            name: '边城',
            author: '明日科技',
            img: require('../../public/assets/images/12.jpg')
          }
        ],
        [
          {
            name: '零基础学C++',
            author: '明日科技',
            img: require('../../public/assets/images/4.jpg')
          },
          {
            name: 'java并发编程实战',
            author: 'Brian Goetz',
            img: require('../../public/assets/images/3.jpg')
          },
        ],
        [
          {
            name: '零基础学Python',
            author: '明日科技',
            img: require('../../public/assets/images/6.jpg')
          },
          {
            name: 'C#从入门到精通',
            author: '明日科技',
            img: require('../../public/assets/images/5.jpg')
          },
        ],
        [
          {
            name: '看漫画学Python',
            author: '赵大羽',
            img: require('../../public/assets/images/8.jpg')
          },
          {
            name: '程序员数学',
            author: '保罗·奥兰德',
            img: require('../../public/assets/images/7.jpg')
          },
        ]
      ],
      data4: [
        [
          {
            name: '边城',
            author: '明日科技',
            img: require('../../public/assets/images/12.jpg')
          },
          {
            name: '文学是什么',
            author: '明日科技',
            img: require('../../public/assets/images/13.jpg')
          },
        ],
        [
          {
            name: '零基础学Python',
            author: '明日科技',
            img: require('../../public/assets/images/6.jpg')
          },
          {
            name: 'C#从入门到精通',
            author: '明日科技',
            img: require('../../public/assets/images/5.jpg')
          },
        ],
        [
          {
            name: '零基础学C++',
            author: '明日科技',
            img: require('../../public/assets/images/4.jpg')
          },
          {
            name: 'java并发编程实战',
            author: 'Brian Goetz',
            img: require('../../public/assets/images/3.jpg')
          },
        ],
        [
          {
            name: '看漫画学Python',
            author: '赵大羽',
            img: require('../../public/assets/images/8.jpg')
          },
          {
            name: '程序员数学',
            author: '保罗·奥兰德',
            img: require('../../public/assets/images/7.jpg')
          },
        ]
      ],
      data5: [
        [
          {
            name: '魔兽战神',
            author: '明日科技',
            img: require('../../public/assets/images/9.jpg')
          },
          {
            name: '笑面难为',
            author: '明日科技',
            img: require('../../public/assets/images/10.jpg')
          },
        ],
        [
          {
            name: '狂人日记',
            author: '鲁迅',
            img: require('../../public/assets/images/11.jpg')
          },
          {
            name: '边城',
            author: '明日科技',
            img: require('../../public/assets/images/12.jpg')
          }
        ],
        [
          {
            name: '零基础学C++',
            author: '明日科技',
            img: require('../../public/assets/images/4.jpg')
          },
          {
            name: '看漫画学Python',
            author: '赵大羽',
            img: require('../../public/assets/images/8.jpg')
          },
        ],
        [
          {
            name: 'java并发编程实战',
            author: 'Brian Goetz',
            img: require('../../public/assets/images/3.jpg')
          },
          {
            name: '程序员数学',
            author: '保罗·奥兰德',
            img: require('../../public/assets/images/7.jpg')
          },
        ]
      ]
    }
  },
  methods: {
    segmentChanged(e: CustomEvent) {
      this.svalue = e.detail.value;
    },
    getRankClass(rank: number) {
      if (rank <= 3) return 'rank-top';
      return 'rank-normal';
    }
  },
  setup() {
    const router = useRouter();
    return { router };
  },
});
</script>

<style lang="less" scoped>
.modern-header {
  background: transparent;
}

.gradient-toolbar {
  --background: linear-gradient(135deg, #4f46e5, #06b6d4);
  --color: white;
  border-radius: 0 0 24px 24px;
  box-shadow: 0 4px 20px rgba(79, 70, 229, 0.3);
}

.page-title {
  text-align: center;
}

.title-content {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.title-text {
  font-size: 1.5rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.title-subtitle {
  font-size: 0.875rem;
  opacity: 0.9;
  font-weight: 400;
}

.modern-content {
  --background: linear-gradient(180deg, #f8fafc 0%, #f1f5f9 100%);
}

.search-container {
  padding: 16px;
  margin-top: -20px;
  position: relative;
  z-index: 10;
}

.modern-searchbar {
  --background: rgba(255, 255, 255, 0.95);
  --border-radius: 16px;
  --box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  --color: #374151;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.ranking-card {
  margin: 0 16px 16px 16px;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  background: rgba(255, 255, 255, 0.95);
}

.card-header {
  padding: 20px 20px 16px 20px;
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.trophy-icon {
  font-size: 24px;
  color: #f59e0b;
}

.card-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: #1f2937;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 8px;
  transition: all 0.3s ease;

  &:hover {
    background: rgba(79, 70, 229, 0.1);
  }
}

.more-text {
  font-size: 0.875rem;
  color: #4f46e5;
  font-weight: 500;
}

.more-icon {
  font-size: 16px;
  color: #4f46e5;
}

.card-content {
  padding: 0 20px 20px 20px;
}

.modern-segment {
  --background: #f3f4f6;
  border-radius: 12px;
  padding: 4px;
  margin-bottom: 20px;
}

.segment-btn {
  --color: #6b7280;
  --color-checked: #ffffff;
  --background-checked: #4f46e5;
  --border-radius: 8px;
  font-weight: 500;
  margin: 2px;
  transition: all 0.3s ease;
}

.books-container {
  min-height: 400px;
}

.book-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.book-row {
  display: flex;
  gap: 16px;
}

.book-item {
  flex: 1;
}

.book-card {
  display: flex;
  background: white;
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: pointer;
  border: 1px solid #e5e7eb;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  }
}

.book-image-container {
  position: relative;
  margin-right: 12px;
}

.book-image {
  width: 60px;
  height: 80px;
  border-radius: 8px;
  object-fit: cover;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.book-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.book-card:hover .book-overlay {
  opacity: 1;
}

.view-icon {
  color: white;
  font-size: 24px;
}

.book-info {
  display: flex;
  flex: 1;
  gap: 8px;
}

.rank-badge {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 12px;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
  margin-top: 4px;

  &.rank-top {
    background: linear-gradient(135deg, #f59e0b, #ef4444);
    box-shadow: 0 2px 8px rgba(245, 158, 11, 0.4);
  }

  &.rank-normal {
    background: #6b7280;
  }
}

.book-details {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  flex: 1;
}

.book-title {
  font-size: 14px;
  font-weight: 600;
  color: #1f2937;
  line-height: 1.3;
  margin-bottom: 4px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-author {
  font-size: 12px;
  color: #6b7280;
  margin-bottom: 8px;
}

.book-stats {
  display: flex;
  align-items: center;
  gap: 4px;
}

.stat-icon {
  font-size: 14px;
  color: #ef4444;
}

.stat-text {
  font-size: 12px;
  color: #6b7280;
  font-weight: 500;
}

@media (prefers-color-scheme: dark) {
  .gradient-toolbar {
    --background: linear-gradient(135deg, #3730a3, #0891b2);
  }

  .modern-content {
    --background: linear-gradient(180deg, #0f172a 0%, #1e293b 100%);
  }

  .ranking-card {
    background: rgba(30, 41, 59, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
  }

  .card-title {
    color: #f1f5f9;
  }

  .book-card {
    background: #334155;
    border: 1px solid #475569;
  }

  .book-title {
    color: #f1f5f9;
  }

  .modern-segment {
    --background: #334155;
  }
}

@media (max-width: 768px) {
  .book-row {
    flex-direction: column;
    gap: 12px;
  }
  
  .book-card {
    padding: 16px;
  }
  
  .book-image {
    width: 50px;
    height: 70px;
  }
}
</style>
